﻿myApp.controller('LawyerFeeCalculator', function ($scope, $http, ngTableParams, $filter, $templateCache) {
    $scope.$emit('newPageLoaded', { 'title': 'Penang Realty BUY OR RENT WITH US 世界地產 Legal Fee & Stamp Duty Calculator', 'description': 'Free online lawyer fee & stamp duty calculator. Try it now.' });


    $scope.calculate = function () {
        validation();
        if ($scope.warning.length > 0)
            return;
        else
            $scope.warning = null;

        $scope.snpLegalfee = 0;
        var snpLegalFeeDisbursement = 800;

        $scope.propertyPrice = $scope.inputPropertyPrice;

        if ($scope.propertyPrice <= 150000) {
            $scope.snpLegalfee = Math.max(0.01 * $scope.propertyPrice, 300);
        }
        else if ($scope.propertyPrice <= 1000000) {
            $scope.snpLegalfee = 150000 * 0.01 + ($scope.propertyPrice - 150000) * 0.007;
        }
        else if ($scope.propertyPrice <= 3000000) {
            $scope.snpLegalfee = 150000 * 0.01 + (850000) * 0.007 + ($scope.propertyPrice - 1000000) * 0.006;

        }
        else if ($scope.propertyPrice <= 5000000) {
            $scope.snpLegalfee = 150000 * 0.01 + (850000) * 0.007 + (2000000) * 0.006 + ($scope.propertyPrice - 3000000) * 0.005;

        }
        else if ($scope.propertyPrice <= 7500000) {
            $scope.snpLegalfee = 150000 * 0.01 + (850000) * 0.007 + (2000000) * 0.006 + (2000000) * 0.005 + ($scope.propertyPrice - 5000000) * 0.004;
        }
        else {
            $scope.snpLegalfee = 150000 * 0.01 + (850000) * 0.007 + (2000000) * 0.006 + (2000000) * 0.005 + (2500000) * 0.004 + ($scope.propertyPrice - 7500000) * 0.003;
        }
        //alert("SNP LEGAL FEE: " + $scope.snpLegalfee);
        $scope.snpLegalfee += ($scope.snpLegalfee * 0.06) + snpLegalFeeDisbursement;




        $scope.snpStampDuty = 0;
        if ($scope.propertyPrice <= 100000) {
            $scope.snpStampDuty = $scope.propertyPrice * 0.01;
        }
        else if ($scope.propertyPrice <= 500000) {
            $scope.snpStampDuty = (100000 * 0.01) + ($scope.propertyPrice - 100000) * 0.02;
        }
        else {
            $scope.snpStampDuty = 100000 * 0.01 + (400000) * 0.02 + ($scope.propertyPrice - 500000) * 0.03;
        }
        $scope.snpTotal = $scope.snpLegalfee + $scope.snpStampDuty;



        $scope.loanLegal = 0;
        var loanLegalFeeDisbursement = 1000;

        $scope.propertyPrice = $scope.inputPropertyPrice;
        $scope.loanAmount = $scope.propertyPrice * ( $scope.propertyMOF / 100 );

        $scope.propertyPrice = $scope.loanAmount;
        if ($scope.propertyPrice <= 150000) {
            $scope.loanLegal = Math.max(0.01 * $scope.propertyPrice, 300);
        }
        else if ($scope.propertyPrice <= 1000000) {
            $scope.loanLegal = 150000 * 0.01 + ($scope.propertyPrice - 150000) * 0.007;
        }
        else if ($scope.propertyPrice <= 3000000) {
            $scope.loanLegal = 150000 * 0.01 + (850000) * 0.007 + ($scope.propertyPrice - 1000000) * 0.006;
        }
        else if ($scope.propertyPrice <= 5000000) {
            $scope.loanLegal = 150000 * 0.01 + (850000) * 0.007 + (2000000) * 0.006 + ($scope.propertyPrice - 3000000) * 0.005;
        }
        else if ($scope.propertyPrice <= 7500000) {
            $scope.loanLegal = 150000 * 0.01 + (850000) * 0.007 + (2000000) * 0.006 + (2000000) * 0.005 + ($scope.propertyPrice - 5000000) * 0.004;
        }
        else {
            $scope.loanLegal = 150000 * 0.01 + (850000) * 0.007 + (2000000) * 0.006 + (2000000) * 0.005 + (2500000) * 0.004 + ($scope.propertyPrice - 7500000) * 0.003;
        }
        $scope.loanLegal += ($scope.loanLegal * 0.06) + loanLegalFeeDisbursement;

        $scope.loanStampDuty = $scope.propertyPrice * 0.005;
        $scope.loanTotal = $scope.loanLegal + $scope.loanStampDuty;

        $scope.total = $scope.loanTotal + $scope.snpTotal;




    }

    function validation() {
        $scope.warning = [];
        if (!$scope.inputPropertyPrice)
            $scope.warning.push({ msg: "Purchase price must not be empty!" })


    }



});